#pragma once
    
#include "common.h"

#include <QtGui/QMainWindow>
#include <QtCore/QSet>

class GUI_DLL_EXPORT Viewer : public QWidget
{
    Q_OBJECT

public:
    Viewer(QWidget *parent);
    ~Viewer();
    void setStorage(class ObjectStorage *storage);
    void paintEvent(QPaintEvent *event);
    QSize sizeHint() const;

    void zoom(QPoint stablePoint, double zoomFactor);
    void scrollTo(QPointF from, QPointF to);

	void pointSelectEvent(const QPointF &pt);

	void addCosmetics(const class MapObject *cosmetic);
	void removeCosmetics(const class MapObject *cosmetic);

    ObjectStorage * getStorage()
    {
        return storage_;
    }

signals:
    void pointClicked(const QPointF &pt);
    void mapLoaded();
    
private:
    void lockRepaint();
    void unlockRepaint();
    QMatrix getMap();

	template <class T> T logicToView(const T &x)
	{
		return getMap().inverted().map(x);
	}

	template <class T> T viewToLogic(const T &x)
	{
		return getMap().map(x);
	}

    class ObjectStorage *storage_;    
	QSet<const class MapObject *> cosmetics;
    double scale;
    QPointF offset;
};

class GUI_DLL_EXPORT MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();
    ~MainWindow();
    Viewer *getViewer();

public slots:
    void loadStorage();
    void zoomIn();
    void zoomOut();
    void drag();
	void select();
	void search();

signals:
	void searchToolSelected ();

private:
    void setTool(class UserTool *tool);
    Viewer *viewer;
    class UserTool *tool;
};
